package com.example.day01.cacluatue;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.example.day01.R;

public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
    TextView addition,subtraction,multiplication,division;//各个运算显示文本
    CheckBox cb_1,cb_2,cb_3,cb_4;
    EditText et_first,et_second;
    int first;
    int second;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        initView();
    }

    private void initView() {
        addition=findViewById(R.id.addition);
        subtraction=findViewById(R.id.subtraction);
        multiplication=findViewById(R.id.multiplication);
        division=findViewById(R.id.division);
        cb_1=findViewById(R.id.cb_1);
        cb_1.setOnClickListener(this);
        cb_2=findViewById(R.id.cb_2);
        cb_2.setOnClickListener(this);
        cb_3=findViewById(R.id.cb_3);
        cb_3.setOnClickListener(this);
        cb_4=findViewById(R.id.cb_4);
        cb_4.setOnClickListener(this);
        et_first=findViewById(R.id.et_first_num);
        et_second=findViewById(R.id.et_second_num);

    }

    @Override
    public void onClick(View v) {
        first = Integer.parseInt(et_first.getText().toString());
        second = Integer.parseInt(et_second.getText().toString());
        switch (v.getId()) {
            case R.id.cb_1:
                int add=first+second;
                addition.setText(first+"+"+second+"="+add);
                addition.setVisibility(View.VISIBLE);
                break;
            case R.id.cb_2:
                int reduce=first-second;
                subtraction.setText(first+"-"+second+"="+reduce);
                subtraction.setVisibility(View.VISIBLE);
                break;
            case R.id.cb_3:
                int ride=first*second;
                multiplication.setText(first+"*"+second+"="+ride);
                multiplication.setVisibility(View.VISIBLE);
                break;
            case R.id.cb_4:
                int except=first/second;
                division.setText(first+"/"+second+"="+except);
                division.setVisibility(View.VISIBLE);
                break;
        }

    }
}
